<template>
    <div class="articles">
        <banner></banner>
        <div class="site-content animate">
            <!-- 文章目录 -->
            <div id="article-menus" v-show="tocs.length > 0">
                <div v-for="toc in tocs" :key="toc.href">
                    <div style="line-height: 1.7em;">
                        <a @click="scrollToPosition(toc.href)"
                           v-html="toc.name"
                           :style="'padding-left:'+(5+toc.h*5) +'px;font-weight:bold;font-size:'+(17-0.3*toc.h)+'px'"></a>
                    </div>
                </div>
            </div>
            <main class="site-main">
                <article class="hentry">
                    <!-- 文章头部 -->
                    <header class="entry-header">
                        <!-- 标题输出 -->
                        <h1 class="entry-title">{{article.title}}</h1>
                        <hr>
                        <div class="breadcrumbs">
                            <div id="crumbs">最后更新时间：{{article.updateTime}}</div>
                        </div>
                    </header>
                    <!-- 正文输出 -->
                    <div>
                        <mavon-editor
                                v-model="article.markerdownMessage"
                                :toolbarsFlag="false"
                                :subfield="false"
                                defaultOpen="preview"
                                ref="markdown"
                        ></mavon-editor>
                    </div>
                    <!-- 文章底部 -->
                    <section-title>
                        <footer class="post-footer">
                            <!-- 阅读次数 -->
                            <div class="post-like">
                                <i class="iconfont iconeyes"></i>
                                <span class="count">{{article.count}}</span>
                            </div>
                            <div class="donate" @click="showDonate=!showDonate">
                                <span>赏</span>
                                <ul class="donate_inner" :class="{'show':showDonate}">
                                    <li class="wedonate"><img src="../assets/img/weixin_pay.png">
                                        <p>微信</p></li>
                                    <li class="alidonate"><img src="../assets/img/ali_pay.png">
                                        <p>支付宝</p></li>
                                </ul>
                            </div>
                            <!-- 文章标签 -->
                            <div class="post-tags">
                                <i class="iconfont iconcategory"></i>
                                <template v-for="item of article.tagList">
                                    <el-tag type="info" size="small" style="margin-right: 10px">{{item.tagName}}
                                    </el-tag>
                                </template>
                            </div>
                        </footer>
                    </section-title>

                    <!--声明-->
                    <div class="open-message">
                        <p>声明：异色瞳的小猫博客|版权所有，违者必究|如未注明，均为原创|本网站采用<a
                                href="https://creativecommons.org/licenses/by-nc-sa/3.0/" target="_blank">BY-NC-SA</a>协议进行授权
                        </p>
                        <p>转载：转载请注明原文链接 - <a href="/">{{article.title}}</a></p>
                    </div>
                    <!--评论-->
                    <div class="comments">
                        <div >
                            <comment-message-editor :inline="true" buttonText="留言" :placeholder="'留下你的评论吧'" ></comment-message-editor>
                        </div>
                        <comment v-for="item in comments" :key="item.id" :comment="item">
                            <template v-if="item.replyList.length">
                                <comment v-for="reply in item.replyList" :key="reply.id" :comment="reply"></comment>
                            </template>
                        </comment>
                    </div>
                </article>
            </main>
        </div>
    </div>
</template>

<script>
    import Banner from '@/components/banner'
    import sectionTitle from '@/components/section-title'
    import comment from '@/components/comment'
    import {findById,findCommentsByArticleId} from "@/api/article"
    import {offsetDomTop, scrolltoToc} from "../utils/index.js";
    import commentMessageEditor from 'comment-message-editor'

    export default {
        name: 'articles',
        data() {
            return {
                showDonate: false,
                comments: [
                    // {
                    //     "comment": {
                    //         "id": 117,
                    //         "postId": "",
                    //         "parentId": "",
                    //         "fromUserId": "",
                    //         "fromUserName": "郭秀英",
                    //         "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhBDO.jpg",
                    //         "content": "次更得连基林响事极自条打代共万又克己这而族连式党强到那特其养不之家使会",
                    //         "createTime": 1171735114596,
                    //         "toUserId": null,
                    //         "toUserName": "黎静",
                    //         "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhoVg.jpg"
                    //     },
                    //     "reply": [
                    //         {
                    //             "id": 122,
                    //             "postId": "",
                    //             "parentId": "",
                    //             "fromUserId": "",
                    //             "fromUserName": "曹杰",
                    //             "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhBDO.jpg",
                    //             "content": "离车称真适资业都始子元眼时美教做导治千员受次政方任经思此工军样保养活队务类研根",
                    //             "createTime": 899931186587,
                    //             "toUserId": 117,
                    //             "toUserName": "郭秀英",
                    //             "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhoVg.jpg"
                    //         },
                    //         {
                    //             "id": 123,
                    //             "postId": "",
                    //             "parentId": "",
                    //             "fromUserId": "",
                    //             "fromUserName": "孟刚",
                    //             "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YD4FR1.jpg",
                    //             "content": "里务研查去育米一他号量提行到己现路属方张难法把整参产白江别切清当效性适现清况教六维北常常路张年子老",
                    //             "createTime": 800347002187,
                    //             "toUserId": 117,
                    //             "toUserName": "郭秀英",
                    //             "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhagx.jpg"
                    //         },
                    //         {
                    //             "id": 124,
                    //             "postId": "",
                    //             "parentId": "",
                    //             "fromUserId": "",
                    //             "fromUserName": "侯秀兰",
                    //             "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhU81.jpg",
                    //             "content": "上带容由备中等族产图思明整意况方二装战就类着",
                    //             "createTime": 177546762627,
                    //             "toUserId": 117,
                    //             "toUserName": "郭秀英",
                    //             "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhBDO.jpg"
                    //         }
                    //     ]
                    // },
                    // {
                    //     "comment": {
                    //         "id": 118,
                    //         "postId": "",
                    //         "parentId": "",
                    //         "fromUserId": "",
                    //         "fromUserName": "康丽",
                    //         "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YD4FR1.jpg",
                    //         "content": "西器率天老北院件算入个领证感身选要动素放其度精公程速器别式什反万必记青复好七划以小小与现没来",
                    //         "createTime": 1158075104375,
                    //         "toUserId": null,
                    //         "toUserName": "常娟",
                    //         "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhagx.jpg"
                    //     },
                    //     "reply": [
                    //         {
                    //             "id": 125,
                    //             "postId": "",
                    //             "parentId": "",
                    //             "fromUserId": "",
                    //             "fromUserName": "苏娜",
                    //             "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhoVg.jpg",
                    //             "content": "十严质更论办真王还料设养原类出国格广界么步你线响正切问制片元习我又几张果东命具国响院安林因",
                    //             "createTime": 602504906471,
                    //             "toUserId": 118,
                    //             "toUserName": "康丽",
                    //             "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YD4FR1.jpg"
                    //         }
                    //     ]
                    // }
                ],
                menus: [],
                content: '',
                article: {},
                // 目录
                tocs: []
            }
        },
        components: {
            Banner,
            sectionTitle,
            comment,
            commentMessageEditor
        },
        methods: {
            getBookInfo() {
                findById(this.$route.params.id).then((res) => {
                    this.article = res.data
                })
            },
            getCommentsById() {
                findCommentsByArticleId(this.$route.params.id).then((res) => {
                    console.log(res)
                    this.comments = res.data
                })
            },
            createdMenusNew() {
                const aArr = this.$refs.markdown.$refs.vShowContent.querySelectorAll("a");
                let tocs = [];
                for (var i = 0; i < aArr.length; i++) {
                    if (aArr[i].id) {
                        let href = aArr[i].id;
                        let name = aArr[i].parentNode.innerText;
                        tocs.push({
                            // href: "#" + href, jqurey id选择器需要#
                            h: Number(aArr[i].parentNode.tagName.substring(1)),
                            href: href,
                            name,
                        });
                    }
                }
                this.tocs = tocs;
            },
    scrollToPosition(id) {
        let position = offsetDomTop(document.getElementById(id));
        //160是因为有着导航条的高度
        position.top = position.top - 160;
        scrolltoToc(position.top);
    }
        },
        mounted() {
            const that = this
            setTimeout(function () {
                that.createdMenusNew()
            }, 500)
        },
        created() {
            document.body.scollTop = 0
            this.getBookInfo()
            this.getCommentsById()
        }
    }
</script>
<style scoped lang="less">
    .site-content {
        position: relative;

        .site-main {
            padding: 80px 0 100px 0;
            width: 115%;
        }
    }

    #article-menus {
        position: sticky;
        top: 0;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .1);
        border-radius: 3px;
        padding: 15px;
        width: 300px;
        transform: translateX(-120%) translateY(150px);
        font-size: 14px;
    }

    article.hentry {
        .entry-header {
            .entry-title {
                font-size: 23px;
                font-weight: 600;
                color: #737373;
                margin: 0.67em 0;

                &:before {
                    content: "#";
                    margin-right: 6px;
                    color: #d82e16;
                    font-size: 20px;
                    font-weight: 600;
                }
            }

            hr {
                height: 1px;
                border: 0;
                background: #EFEFEF;
                margin: 15px 0;
            }

            .breadcrumbs {
                font-size: 14px;
                color: #D2D2D2;
                text-decoration: none;
                margin-bottom: 30px;
            }
        }

        footer.post-footer {
            width: 100%;
            padding: 20px 10px;
            margin-top: 30px;
            height: 65px;
            position: relative;

            i {
                font-size: 18px;
                margin-right: 5px;
            }

            .post-like {
                float: right;
                margin: 7px 0 0 20px;
            }

            .post-share {
                float: right;
                list-style: none;
                margin-right: 20px;
            }

            .donate {
                float: left;
                line-height: 36px;
                border-radius: 100%;
                -webkit-border-radius: 100%;
                -moz-border-radius: 100%;
                border: 1px solid #2B2B2B;

                &:hover {
                    border: 1px solid goldenrod;

                    span {
                        color: goldenrod;
                    }
                }

                span {
                    color: #2B2B2B;
                    padding: 10px;
                    position: relative;
                    cursor: pointer;
                }

                .donate_inner {
                    display: none;
                    margin: 0;
                    list-style: none;
                    position: absolute;
                    left: 80px;
                    top: -40px;
                    background: #FFF;
                    padding: 10px;
                    border: 1px solid #ddd;
                    box-shadow: 0 2px 6px rgba(0, 0, 0, .08);
                    border-radius: 3px;

                    &.show {
                        display: block;
                    }

                    li {
                        float: left;
                    }

                    img {
                        width: 100px;
                    }

                    p {
                        text-align: center;
                        font-size: 15px;
                        color: #D2D2D2;
                        line-height: 1rem;
                    }
                }

                .donate_inner:after, .donate_inner:before {
                    content: "";
                    position: absolute;
                    left: 0;
                    bottom: 45%;
                    margin-left: -8px;
                    border-top: 8px solid transparent;
                    border-bottom: 8px solid transparent;
                    border-right: 8px solid #fff;
                }

                .donate_inner:before {
                    left: -1px;
                    border-right: 8px solid #ddd;
                }

            }

            .post-tags {
                margin: 7px 0 0 20px;
                float: left;
                text-transform: uppercase;

                a:hover {
                    color: #ff6d6d;
                }
            }
        }

        .open-message {
            margin: 50px 0;
            position: relative;
            background: #2B2B2B;
            padding: 10px 30px;
            border-radius: 3px;
            font-size: 14px;
            color: #fff;

            &:after {
                content: "";
                border-left: 10px solid transparent;
                border-right: 10px solid transparent;
                border-bottom: 10px solid #2B2B2B;
                position: absolute;
                top: -8px;
                left: 48%;
            }

            p {
                margin: 10px 0;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            a {
                color: #A0DAD0;
                padding: 0 5px;
            }
        }
    }

    @media (max-width: 1490px) {
        #article-menus {
            display: none;
        }
    }

    @media (max-width: 800px) {
        #article-menus {
            display: none;
        }
    }
</style>
